
import bean.Pemesanan;
import bean.Penerimaan;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import oracle.jdbc.pool.OracleDataSource;

@WebServlet(name = "TerimaBarang", urlPatterns = {"/TerimaBarang"})
public class TerimaBarang extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

//        Penerimaan terima = new Penerimaan();
//        
//        String jumlah_barang = request.getParameter("jumlahbarang");
//        String harga_barang = request.getParameter("hargabarang");
//        String id_barang = request.getParameter("idbarang");
//        
//        if ("".equals(jumlah_barang) || "".equals(harga_barang) || "".equals(id_barang)) {
//            RequestDispatcher requestDispatcher = request.getRequestDispatcher("form_terima_barang.jsp");
//            requestDispatcher.forward(request, response);
//        } else {
//            int jum = Double.parseDouble(jumlah_barang);
//            int jum = Integer.parseInt(jumlah_barang);
//             double harga = Double.parseDouble(harga_barang);
//             
//            terima.setJumlahBarang(jum);
//            terima.setHargaBarang(harga);
//            terima.setIdBarang(id_barang);
//            
//            Cookie[] ck = request.getCookies();
//            terima.setUsername(ck[1].getValue());

        Pemesanan pesan = new Pemesanan();
        Connection connection = null;
        OracleDataSource ds;
        try {
            ds = new OracleDataSource();
             ds.setURL("jdbc:oracle:thin:@172.23.9.185:1521:orcl");
                ds.setPassword("mhs115314034");
                ds.setUser("mhs115314034");
            connection = ds.getConnection();
            Statement sta = connection.createStatement();
            ResultSet rs = sta.executeQuery("select * from pemesanan");

            while (rs.next()) {
                sta.executeQuery("insert into penerimaan values ('"
                        + rs.getString("KODEPEMESANAN") + "','"
                        + rs.getString("WAKTUPEMESANAN") + "','"
                        + rs.getString("JUMLAHBARANG") + "','"
                        + rs.getString("USERNAME") + "','"
                        + rs.getString("PEMESAN") + "')");
            }

//                Statement statement = connection.createStatement();
//                statement.executeQuery("insert into pemesanan values ('"
//                        + pesan.getKodePemesanan() + "','"
//                        + pesan.getWaktuPemesanan() + "','"
//                        + pesan.getJumlahBarang() + "','"
//                        + pesan.getUsername() + "','"
//                        + pesan.getPemesan() + "')");
                System.out.println("data berhasil diinputkan");
        } catch (SQLException ex) {
            Logger.getLogger(Pemesanan.class.getName()).log(Level.SEVERE, null, ex);
        }

        out.println("<html>");
        out.println("<head>"
                + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"
                + "<title>Login</title></head>");
        out.println("<body bgcolor='blue'>"
                + "<table border='0' align='center'>"
                + "<th colspan='2'rowspan='1' width='1%' bgcolor='#1E1C1C' ><h1> <font color='white'"
                + "<h1 align='center' style='color:#00CCFF'> Rekayasa Perangkat Lunak Berorientasi Obyek </h1>"
                + "<h2 align='center' style='color:#00CCFF'> Sistem Informasi Distribusi Aqua ' Toko Mentari ' </h2>"
                + "<h3 align='center' style='color:#00CCFF'> Kelompok JBK+ </h3>"
                + "</font>"
                + " </table>");
        out.println("<form action=\"form_utama_gudang.jsp\">"
                + "<table align=\"center\" border=\"0\">");
        out.println("<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"kembali\" value=\"kembali ke form utama\" width=\"250\"/></td>"
                + "</tr></table></form>");
        out.println("</body>");
        out.println("</html>");
    }
//    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
